import { BasicColumn, FormSchema } from '/@/components/Table';
import { h } from 'vue';
import { Switch, Tag } from 'ant-design-vue';
import { setClientStatus } from './client.api';
import { useMessage } from '/@/hooks/web/useMessage';
import { DictCodeEnum, getDictItems, getDictItemMap } from '/@/utils/dict';

// 授权方式字典数据
const grantTypeDict = getDictItems(DictCodeEnum.GRANT_TYPE);

// 授权范围字典数据
const clientScopeDict = getDictItems(DictCodeEnum.CLIENT_SCOPE);
const clientScopeDictMap = getDictItemMap(DictCodeEnum.CLIENT_SCOPE);

// 授权方式字典翻译
function authorizedGrantTypesFormat(grantTypes) {
  if (!grantTypes) return;

  const temp: string[] = [];
  grantTypes.split(',').forEach((type) => {
    const types = grantTypeDict.filter((item) => item.value == type);
    if (types && types.length > 0) {
      temp.push(types[0].label);
    }
  });
  return temp.join(' | ');
}

export const columns: BasicColumn[] = [
  {
    title: '客户端ID',
    dataIndex: 'clientId',
    width: 200,
  },
  {
    title: '客户端密钥',
    dataIndex: 'clientSecret',
    width: 180,
  },
  {
    title: '授权范围',
    dataIndex: 'scope',
    width: 80,
    dictFormat: clientScopeDictMap,
  },
  {
    title: '授权方式',
    dataIndex: 'authorizedGrantTypes',
    customRender: ({ text }) => {
      return authorizedGrantTypesFormat(text);
    },
  },
  {
    title: '认证令牌时效',
    dataIndex: 'accessTokenValidity',
    width: 120,
  },
  {
    title: '刷新令牌时效',
    dataIndex: 'refreshTokenValidity',
    width: 120,
  },
  {
    title: '是否自动放行',
    dataIndex: 'autoApprove',
    width: 120,
    customRender: ({ record }) => {
      const autoApprove = record.autoApprove;
      const enable = ~~autoApprove === 1;
      const color = enable ? 'green' : 'red';
      const text = enable ? '是' : '否';
      return h(Tag, { color: color }, () => text);
    },
  },
  {
    title: '状态',
    dataIndex: 'status',
    width: 120,
    customRender: ({ record }) => {
      if (!Reflect.has(record, 'pendingStatus')) {
        record.pendingStatus = false;
      }
      return h(Switch, {
        checked: record.status === 1,
        checkedChildren: '已启用',
        unCheckedChildren: '已禁用',
        loading: record.pendingStatus,
        onChange(checked: boolean) {
          record.pendingStatus = true;
          const newStatus = checked ? 1 : 0;
          const { createMessage } = useMessage();
          setClientStatus(record.id, newStatus)
            .then(() => {
              record.status = newStatus;
              createMessage.success(`已成功修改客户端状态`);
            })
            .catch(() => {
              createMessage.error('修改客户端状态失败');
            })
            .finally(() => {
              record.pendingStatus = false;
            });
        },
      });
    },
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'clientId',
    label: '客户端ID',
    component: 'Input',
    colProps: { span: 8 },
  },
  {
    field: 'clientId',
    label: '客户端名称',
    component: 'Input',
    colProps: { span: 8 },
  },
];

export const formSchema: FormSchema[] = [
  {
    field: 'id',
    label: 'ID',
    component: 'Input',
    show: false,
  },
  {
    field: 'clientId',
    label: '客户端ID',
    required: true,
    component: 'Input',
  },
  {
    field: 'clientSecret',
    label: '客户端密钥',
    required: true,
    component: 'Input',
  },
  {
    field: 'scope',
    label: '授权范围',
    required: true,
    component: 'RadioGroup',
    defaultValue: 'all',
    componentProps: {
      options: clientScopeDict,
    },
  },
  {
    field: 'autoApprove',
    label: '自动放行',
    component: 'RadioButtonGroup',
    defaultValue: 0,
    componentProps: {
      options: [
        { label: '否', value: 0 },
        { label: '是', value: 1 },
      ],
    },
  },
  {
    field: 'accessTokenValidity',
    label: '过期时间',
    component: 'Input',
  },
  {
    field: 'refreshTokenValidity',
    label: '失效时间',
    component: 'Input',
  },
  {
    field: 'webServerRedirectUri',
    label: '回调地址',
    component: 'Input',
  },
  {
    field: 'authorities',
    label: '权限列表',
    component: 'Input',
  },
  {
    field: 'authorizedGrantTypes',
    label: '授权方式',
    required: true,
    component: 'CheckboxGroup',
    componentProps: {
      options: grantTypeDict,
    },
    colProps: { lg: 24, md: 24 },
  },
  {
    label: '扩展信息',
    field: 'additionalInformation',
    component: 'InputTextArea',
    componentProps: {
      placeholder: 'JSON格式',
    },
    colProps: { lg: 24, md: 24 },
  },
];
